<?php 

/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

	global $jbconfig, $permission;
	include_once ("components/$option/models/jacodes.php");
	$model_codes = new JAJobBoardModelJACodes ( );
	JRequest::setVar ( 'field_id', "ja_jobs_apply_type" );
	$arr_apply_type = $model_codes->getItems ();
//	 = JobBoardConstant::getApplyJobOptions();
	$enable_apply_type = $jbconfig['posts']->get('apply_option');
	if ($enable_apply_type)
		$enable_apply_type = explode(',',$enable_apply_type);
	else 
		$enable_apply_type = array('1','3')	;
	$allow_apply_type = '';
	if ($this->item->apply_type){
		$allow_apply_type = explode(',', $this->item->apply_type);
	}
	else 
		$allow_apply_type = $enable_apply_type;
	$allow_apply_type = array_intersect($allow_apply_type,$enable_apply_type);
	
	$tmp = array();
	foreach ($allow_apply_type as $app_type)
		$tmp[] = $app_type;
	if (count($tmp)>0)
		$allow_apply_type = $tmp;
	else 
		$allow_apply_type = $enable_apply_type;
	sort($allow_apply_type);
	
	//Get post back data application
	if (JRequest::getVar('app_postback',false)&&JRequest::getVar('job_id')==$this->item->id)
		$this->application = $this->getPostBack_Apply();
	
	$user = JFactory::getUser();
	$guestApply = 1;
	
	if ( getCurrentUserType() == 'Guest' && empty($user->id) ){
		$guestApply = 0;
		$guest_permission = $jbconfig ['group_permission']->get ( 'Guest_permission', '0' );
		
		if (isset ( $permission ['guest'] ['guest_apply_job'] )) {
			if ( ($guest_permission & $permission ['guest'] ['guest_apply_job']) == $permission ['guest'] ['guest_apply_job']) {
				$guestApply = 1;
			}
		}
	}
?>
<!--<div class="ja-apply-header">
	<span><?php echo JText::_('Job Information:')?></span>
	<strong style="padding-left: 10px;"><?php echo $item->title?></strong>
	<br/>
	<span><?php echo JText::_('Notes:')?></span>	
	<span style="color: red; padding-left: 10px;"><?php echo JText::_('Please send the most recent resume')?></span>
</div>
<br/>
<br/>
-->
<?php if ( $guestApply ) { ?>

<?php if($allow_apply_type){?>
<iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #fff;"></iframe>
<h2><?php echo JText::_('Your Application'); ?> <span style="color: red; font-size: 70%; font-weight: normal;">( <?php echo JText::_('Please send the most recent resume')?> )</span></h2>
<?php }?>

<?php if($allow_apply_type && count($allow_apply_type)>1){?>
 
<div align="left"> 
	<strong><?php echo JText::_('Please select the method of sending application')?>:</strong>
	<?php for ($kk=count($allow_apply_type)-1; $kk>=0; $kk--) {
		$apply_type_name = 'Write Message';
		foreach ($arr_apply_type as $apply_type){
			if ($apply_type->value==$allow_apply_type[$kk])
				$apply_type_name = $apply_type->text;
		}?>
		<span>
			<?php 
			if ((count($allow_apply_type)==1)||(count($allow_apply_type))>1&&$allow_apply_type[$kk]==3)
				$str_checked = 'checked="checked"';
			else 
				$str_checked = '';
		 	?>
			<input onclick="select_apply_type(this.value,'<?php echo $this->item->id; ?>')"  type="radio" name="apply_type_<?php echo $this->item->id; ?>" id="apply_type_<?php echo $allow_apply_type[$kk]; ?>_<?php echo $this->item->id; ?>" value="<?php echo $allow_apply_type[$kk]; ?>" <?php echo $str_checked; ?> />
			<label for="apply_type_<?php echo $allow_apply_type[$kk]; ?>_<?php echo $this->item->id; ?>"><?php echo JText::_($apply_type_name);  ?></label>
		</span> 
	<?php } ?>
</div>
<?php }?>

<?php if($allow_apply_type){?>

<div id="write_message_<?php echo $this->item->id; ?>" <?php echo (in_array('3',$allow_apply_type))?'':'style="display:none;"'; ?>>
<form action="<?php echo JURI::root();?>index.php" method="post" enctype="multipart/form-data" name="applyjob-form-<?php echo $item_count; ?>" id="applyjob-form-<?php echo $item_count; ?>">
    
      <table class="tabledata applicationdetails">
          <!--<tr>
              <td colspan="2">
              <?php 
              if($this->application->id){ 
                  echo JText::_('You have already applied for this job. Please fill in the form below for updating job application');
              } else {
              	if (!JRequest::getVar('app_postback',false))
              	{
                   echo JText::_('Please fill in the form below for job application');
                   $this->application->subject = '';//JText::_('Reply to your ad posted on ') ;
              	}
              	
              		
              }
              ?>                                        
              </td>
          </tr>
          -->
          <?php
            $k=0;
            $action = (isset($this->application->id)&&($this->application->id!=0)&&($this->application->id!=null))?'edit':'new';
      		$firstIndex = findFirstField($this->application,$this->appfieldlist,$action);
			$lastIndex = findLastField($this->application,$this->appfieldlist,$action);
            $hidden = '';
            
			for($i = 0, $n = count($this->appfieldlist); $i < $n; $i++ )
            if ($this->appfieldlist[$i]->field_name!='opt_attachment'||getCurrentUserType()!='Guest')
            {
                $field = $this->appfieldlist[$i];
                $field_name = $field->field_name;
                if($this->application->subject=='')  $this->application->subject = JText::_('Application for').' '. $item->title. ' '. JText::_('via').' '. $jbconfig['emails']->get('sitname'); 
                if ($field->field_type!='FILE'&&$field->field_type!='EDITOR')    
                	$input = rendfield($this->application, $field, false, false,false,'jaapplications',$item_count);
                elseif ($field->field_type=='FILE') 
                {
					$input = '<input size="50" type="text" name="display_'.$field->field_name.'_'.$this->item->id.'" id="display_'.$field->field_name.'_'.$this->item->id.'" value="" readonly />';
                	$input .= '<input type="hidden" name="'.$field->field_name.'_'.$this->item->id.'" id="'.$field->field_name.'_'.$this->item->id.'" value=""  />';
                	$input .= '&nbsp<span>';
                	if (($field->is_required == '1') && (! $field->is_readonly))
                		$input .= '<font color="red">*</font>';
                	$input .= '<a id="'.$field->field_name.'_button'.$item_count.'" class="modal" rel="{handler: \'iframe\', size: {x: 650, y: 225}}" href="index.php?option=com_jajobboard&view=jajobs&layout=jauploadfile&field_name='.$field->field_name.'&field_type='.$field->field_type.'&job_id='.$this->item->id.'&tmpl=component" title="'.JText::_('Upload file').'">';
                	$input .= JText::_('Upload file');
                	$input .= '</a>';
                	$input .= '</span>';
                	
                }
                elseif ($field->field_type=='EDITOR')
                {
                	// $input = '<input type="hidden" id="'.$field->field_name.$item->id.'-editor" name ="'.$field->field_name.$item->id.'-editor" value="" />';
                	$input .= '<textarea style="width:100%; height: 100px" id="'.$field->field_name.$item->id.'" name="'.$field->field_name.$item->id.'"></textarea>';
                	
                	// Load the JEditor object
					$editor = & JFactory::getEditor ();
					
					$field->field_height = $field->field_height ? $field->field_height : 200;
					$field->field_width = $field->field_width ? $field->field_width : 300;
					
					// parameters : areaname, content, width, height, cols, rows, button?					
					// $input = $editor->display ( $field->field_name.$item->id, '', '100%', $field->field_height, '70', '15', false, array ('theme' => 'simple' ) );

					               	
                	if (($field->is_required == '1') && (! $field->is_readonly))
                		$input .= '<font color="red">*</font>';
                }
                if ($i==$firstIndex)	$classname = 'first sectiontableentry'.($k+1);
				elseif ($i==$lastIndex)  		$classname = 'last sectiontableentry'.($k+1);
				else $classname = 'sectiontableentry'.($k+1);
					
                if(  $user->id==0  || ( substr($field_name,0,5)!="guest" && $user->id!=0 )){
	                if($input!='' && $field->display!='hide_on_new')
	                { ?>
		              	<tr class="<?php echo $classname; ?>">
		                  <td  width="30%" valign="top">
		                      <?php 
//		              			if (substr($field_name,0,5)!="guest"){
//	                      			echo JText::_($field->label); 
//	                      		}
//	                      		elseif (getCurrentUserType()=='Guest'){
	                      			echo JText::_($field->label);                
//	                      		}
		                      ?>
		                      
		                  </td>
		                  
		                  <td width="70%" >
		                      <?php 
//	                           	if (substr($field_name,0,5)!="guest")
//	                           	{	
//	                            		echo $input;
//	                           	}
//	                           	elseif (getCurrentUserType()=='Guest'){
	                   				
	                           		echo $input;
//	                           	}
	                           		
	                           ?>
		                  </td>
	        			</tr>                    
			            <?php    $k = 1 - $k;?>
	                <?php }elseif ($input!=''){?>	              	
		              	<?php $hidden .=  $input;?>		              	
		            <?php }?>
              	<?php }?>
          <?php }?>
          <tr>
          	<td>&nbsp;</td>
          	<td>
          		<input type="checkbox" name="confirm_email" id="confirm_email" value="okie" /> &nbsp;
          		<label for="confirm_email"><?php echo JText::_('Notify via email when my application is successfully sent'); ?></label>
          	</td>
          </tr>
          <tr>
          	<td>&nbsp;</td>
          	<td>
          		<input type="checkbox" name="confirm_email_when_read" id="confirm_email_when_read" value="1" /> &nbsp;
          		<label for="confirm_email_when_read"><?php echo JText::_('Notify via email when my application is reviewed'); ?></label>
          	</td>
        </tr>
        <?php if ($jbconfig['posts']->get('enable_verify_code')) { ?>
        <tr>
           	<td>&nbsp;</td>
          	<td>          	
          		<img id="securityImage" src="<?php echo JURI::root().'index.php?option=com_jajobboard&view=jajobs&layout=display_capcha'; ?>" /> 
          		<a href="#" onclick="jacLoadNewCaptcha(); return false;"><img title="Change image" src="<?php echo JURI::root().'/components/com_jajobboard/images/refresh.png'; ?>" style="border:0px;" /></a>
          	</td>
          </tr>
          <tr>
          	<td width="30%">
          		<?php echo JText::_('Security Code'); ?>:
          	</td>
          	<td width="70%">
          		<input id="security_text" name="security_text" type="text" />
          	</td>
          </tr>
          <?php } ?>
          <tr>
          	<td colspan="2">
          		<span style="color:red;font-style:italic;"><?php echo JText::_('Fields marked with an asterisk (*) are required.'); ?></span>
          	</td>
          </tr>
           
          <tr>
              <td>&nbsp;</td>
              <td align="left">
              	  <div id="ja-application-loading" class="" style="float:left; text-align:center"></div>
                  <input id="button-save" class="button"  type="submit" name="button"  value="<?php echo JText::_('Send Application'); ?>" />
                  <input class="button" type="reset" value="<?php echo JText::_('Reset'); ?>" />
              </td>
          </tr>
         
      </table>
       
    <?php if (!$this->application->id)
    {
    	
    ?>
	<input type="hidden" name="created_date" value="<?php echo date('Y-m-d H:i:s'); ?>" />    
    
    
    <?php 
    }
    ?>
    
    <?php echo $hidden;?>
    <input type="hidden" name="apply_type" value="3" />
    <input type="hidden" name="user_id" value="<?php echo $this->application->user_id; ?>" />    
	<input type="hidden" name="job_id" value="<?php echo $this->item->id; ?>" />    
    <input type="hidden" name="cid[]" value="<?php echo $this->application->id; ?>" />
    <input type="hidden" name="posted" value="1" />
    <input type="hidden" name="option" value="<?php echo JBCOMNAME; ?>" />
    <input type="hidden" name="view" value="jajobs" />
    <input type="hidden" name="task" value="save_application" />
    <input type="hidden" name="Itemid" value="<?php echo $this->Itemid; ?>" />
    <input type="hidden" name="tmpl" value="component" />
    <?php echo JHTML::_( 'form.token' ); ?>    
 </form>
</div>
 <div align="center" id="direct_url_<?php echo $this->item->id; ?>"  <?php echo (count($allow_apply_type)==1&&in_array('1',$allow_apply_type))?'':'style="display:none;"'; ?>>
 <br />
 
 <?php if($this->item->direct_url){?>
 	<?php if (substr($this->item->direct_url,0,7)=='http://' || substr($this->item->direct_url,0,8)=='https://' ) {?>
		 <label><?php echo JText::_('Apply now'); ?></label>
		 <a target="_blank" href="<?php echo $this->item->direct_url?$this->item->direct_url:''; ?>"><?php echo $this->item->direct_url?$this->item->direct_url:''; ?></a>
	 <?php } else { ?>
	 	<div  style="text-align:left;">
	 	 <span><?php echo str_replace('\n','<br />',$this->item->direct_url); ?></span>
	 	 </div>
	 <?php } ?>
	 
 <?php }else{?>
 	<span style="color:red"><?php echo JText::_('Sorry. Information has not been updated by Employer.')?></span>
 <?php }?>
</div> 
<script language="javascript" type="text/javascript">
/* <![CDATA[ */

// Refresh capcha Image
function jacLoadNewCaptcha(){	            				
	if($("securityImage") != undefined){		
		$("securityImage").src = "<?php echo JURI::root();?>/index.php?option=com_jajobboard&view=jajobs&layout=display_capcha&type=change&rand=" + Math.random();
	}
}


var multple_apply = <?php echo $jbconfig['posts']->get('allow_apply_several_time',0); ?>;
var user_id = <?php echo $user->id; ?>;
window.addEvent('domready', function() {
	// You can skip the following two lines of code. We need them to make sure demos
	// are runnable on MooTools demos web page.
	if (!window.demo_path) window.demo_path = '';
	var demo_path = window.demo_path;
	// --
	
	$('<?php echo 'applyjob-form-'.$item_count; ?>').addEvent('submit', function(e) {
		//Prevents the default submit event from loading a new page.
		new Event(e).stop();

		//Empty the log and show the spinning indicator.
		var log = $('app_err_<?php echo $this->item->id; ?>').empty()//.addClass('ajax-loading');
		$('ja-application-loading').addClass('ajax-loading');
		$('button-save').disabled = true;
		/**
		 * send takes care of encoding and returns the Ajax instance.
		 * onComplete removes the spinner from the log.
		 */

		this.send({
			update: log,
			method: 'post', 
			onComplete: function(result) {
				save_application_status = Get_Cookie('save_applicaton');
				if (save_application_status=='success')
				{					
					if (!multple_apply && user_id!=0)
					{
						var button_apply = document.getElementById('button_apply_<?php echo $this->item->id; ?>');
						var apply_div = document.getElementById('applyjob_<?php echo $this->item->id; ?>');
						button_apply.style.display = 'none';
						apply_div.style.display='none';
					}
					else
					{
						var apply_div = document.getElementById('applyjob_<?php echo $this->item->id; ?>');
						var apply_form = document.getElementById('<?php echo "applyjob-form-".$item_count;?>');
						apply_form.reset();
						apply_div.style.display='none';
					}
				}
							
				log.innerHTML = result;
				log.style.display = 'block';
				$('ja-application-loading').removeClass('ajax-loading');
				$('button-save').disabled = false;
				var current_href = location.href;
				var post = location.href.indexOf('#');
				if(post>0){
					current_href = current_href.substring(0, post);
				}
				location.href = current_href + "#apply-job-"+<?php echo $this->item->id; ?>;
			}
		});



	});
});

/* ]]> */
</script>
<?php }else{?>
	<div style="border: 1px solid rgb(238, 238, 0); padding: 0.5em; margin-top: 20px; color: red"><?php echo JText::_('Sorry. The application does not support.')?></div>
	
<?php }?>

<?php } else { 
 	$uri		= JFactory::getURI();
	$return		= $uri->toString();
 ?>
<div style="background-color:#FFFFEE; border:1px solid #EEEE00; margin-top:2em; padding:0.5em;">
<span style="color: red">
	<strong>
		<?php echo JText::_('Note'); ?>!
	</strong>
</span>
<div id="remind_login_<?php echo $item->id; ?>">
	<?php echo JText::_('Please sign in to continue...'); ?>
	&nbsp;&nbsp;
	<a class="button" href="<?php echo JRoute::_(JURI::root().'index.php?option=com_user&view=login&return='.base64_encode($return)); ?>"><?php echo JText::_('Login') ?></a>
    <br/><br/>
	<?php echo JText::_("If you haven't account, please "); ?><a href="http://localhost/joomla/ja_jobboard/index.php?option=com_jajobboard&view=jauserregister&layout=regjobseekerform&Itemid=9999"><?php echo JText::_("register"); ?></a> <?php echo JText::_("as a JobSeeker to apply"); ?>	
</div>
</div>
<?php } ?>